package cn.stu.cms.service.impl;

import cn.stu.cms.dto.SetRoleDto;
import cn.stu.cms.mapper.RolesMapper;
import cn.stu.cms.pojo.Roles;
import cn.stu.cms.pojo.Users;
import cn.stu.cms.service.RolesService;
import cn.stu.cms.service.UsersService;
import cn.stu.cms.util.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import static cn.stu.cms.common.CacheConstants.ROUTER_CACHE;
import static cn.stu.cms.common.CommonConstants.*;

/**
 * (Roles)表服务实现类
 *
 * @author gh
 * @date 2023/09/21
 */
@Service
@AllArgsConstructor
public class RolesServiceImpl extends ServiceImpl<RolesMapper, Roles> implements RolesService {

    private final UsersService usersService;

    /**
     * 修改数据
     *
     * @param editRole 编辑角色
     * @return 实例对象
     */
    @Override
    public R<Boolean> edit(Roles editRole) {
        Roles role = lambdaQuery().eq(Roles::getRoleId, editRole.getRoleId()).one();
        if (role == null) {
            return R.failed("角色" + NO_EXIST_MSG);
        }
        if (!role.getRoleName().equals(editRole.getRoleName())) {
            boolean nameExists = lambdaQuery().eq(Roles::getRoleName, editRole.getRoleName()).exists();
            if (nameExists) {
                return R.failed("角色名" + EXIST_MSG);
            }
        }
        updateById(editRole);
        return R.ok(Boolean.TRUE, EDIT_OK_MSG);
    }

    /**
     * 设置身份验证
     *
     * @param setRoleDto 将角色dto设置为
     * @return {@link R}<{@link Boolean}>
     */
    @Override
    public R<Boolean> setAuth(SetRoleDto setRoleDto) {
        boolean roleExists = lambdaQuery().eq(Roles::getRoleId, setRoleDto.getRoleId()).exists();
        if (!roleExists) {
            return R.failed("角色" + NO_EXIST_MSG);
        }
        Users user = usersService.lambdaQuery().eq(Users::getUserId, setRoleDto.getUserId()).one();
        if (user == null) {
            return R.failed("用户" + NO_EXIST_MSG);
        }
        if (ADMIN.equals(user.getAccount())) {
            return R.failed(DISALLOWED_OPERATION);
        }
        usersService.lambdaUpdate().eq(Users::getUserId, setRoleDto.getUserId()).set(Users::getRoleId, setRoleDto.getRoleId()).update();
        return R.ok(EDIT_OK_MSG);
    }

    /**
     * 按ID删除
     * 通过主键删除数据
     *
     * @param roleId 角色ID
     * @return 是否成功
     */
    @Override
    @CacheEvict(value = ROUTER_CACHE, allEntries = true)
    public R<Boolean> del(Long roleId) {
        boolean idExists = lambdaQuery().eq(Roles::getRoleId, roleId).exists();
        if (!idExists) {
            return R.failed("角色" + NO_EXIST_MSG);
        }
        boolean userExist = usersService.lambdaQuery().eq(Users::getRoleId, roleId).exists();
        if (userExist) {
            return R.failed("角色使用中");
        }
        removeById(roleId);
        return R.ok(Boolean.TRUE, DEL_OK_MSG);
    }

    /**
     * 添加
     *
     * @param addRoles 添加角色
     * @return {@link R}
     */
    @Override
    public R<Boolean> add(Roles addRoles) {
        boolean idExists = lambdaQuery().eq(Roles::getRoleId, addRoles.getRoleId()).exists();
        if (idExists) {
            return R.failed("角色ID" + EXIST_MSG);
        }
        boolean nameExists = lambdaQuery().eq(Roles::getRoleName, addRoles.getRoleName()).exists();
        if (nameExists) {
            return R.failed("角色名" + EXIST_MSG);
        }
        baseMapper.insert(addRoles);
        return R.ok(Boolean.TRUE, SAVE_OK_MSG);
    }
}
